<?php
session_start();
require_once("../connect/dbcon.php");

$iddh=date("U");

$tennguoinhan=$_POST["tennguoinhan"];
$diadiemgiaohang=$_POST["DiaDiemGiaoHang"];
$ngaygiaohang=$_POST["Ngay"];

$Query = "insert into donhang(idDH, ThoiDiemDatHang, ThoiDiemGiaoHang, TenNguoiNhan, DiaDiemGiaoHang) 
values('$iddh',CURRENT_DATE(),'$ngaygiaohang','$tennguoinhan','$diadiemgiaohang')";
$kq = mysql_query($Query);

if ($kq)
{ 	  $max = count($_SESSION['GioHang']); 
      for ($i = 0; $i < $max; $i++) { 
	  	$idMay=$_SESSION['GioHang'][$i]['idMay']; 
		$SoLuong=$_SESSION['GioHang'][$i]['SoLuong']; ;
		$Gia=$_SESSION['GioHang'][$i]['Gia']; 

		$Query = "insert into donhangchitiet(idDH, idMay, SoLuong, DonGia ) 
		values('$iddh','$idMay',$SoLuong,$Gia)";
		$Result = mysql_query($Query);
	  }
	  unset($_SESSION['GioHang']);
	  echo "<script language='javascript'>alert('Mua hàng thành công !');location.href='../index.php';</script>";
}
else
{
	 echo "<script language='javascript'>alert('Mua hàng thành công !');location.href='../index.php';</script>";
}



?>
